package com.hanrui.android.activity.message;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.hanrui.android.R;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.fragment.message.supervise.MySuperviseMonthListFragment;
import com.hanrui.android.fragment.message.supervise.MySuperviseWeekListFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的督办
 */
public class MySuperviseActivity extends ActionBarBaseActivity implements BottomNavigationBar.OnTabSelectedListener{


    @BindView(R.id.tab_layout)
    TabLayout tabLayout;

    private FragmentAdapter mFragmentAdapter;

    private List<String> CHANNELS = new ArrayList<>();
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private MySuperviseWeekListFragment weekFragment;
    private MySuperviseMonthListFragment monthFragment;
    List<Fragment> list;


    int item = 0;


    @Override
    protected void initInjector() {}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basetab);
        unbinder = ButterKnife.bind(this);

        item = getIntent().getIntExtra("item",0);

        init();

    }

    private void init() {

        mTitleBar.setTitle("我的督办");


        CHANNELS.add("周督办");
        CHANNELS.add("月督办");

        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        for (String title:CHANNELS){
            tabLayout.addTab(tabLayout.newTab().setText(title));
        }
        weekFragment =  MySuperviseWeekListFragment.newInstance();
        monthFragment =  MySuperviseMonthListFragment.newInstance();
        list = new ArrayList<>();
        list.add(weekFragment);
        list.add(monthFragment);

        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),CHANNELS,list);
        viewPager.setAdapter(mFragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);

        viewPager.setCurrentItem(item);
        tabLayout.getTabAt(item).select();
    }

    @Override
    public void onTabSelected(int position) {

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    public class FragmentAdapter extends FragmentPagerAdapter {

        List<Fragment> fragmentList;
        private List<String> titles ;
        public FragmentAdapter(FragmentManager fm, List<String> titles, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    public static void start(Context context, int item){
        Intent intent = new Intent(context,MySuperviseActivity.class);
        intent.putExtra("item",item);
        context.startActivity(intent);
    }
}
